package com.myblog.controller;

import com.myblog.pojo.Article;
import com.myblog.pojo.IndexArticle;
import com.myblog.pojo.Page;
import com.myblog.pojo.Photo;
import com.myblog.service.Indexservice;
import com.myblog.service.Pageservice;
import com.myblog.service.Photoservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Controller
public class Indexcontroller {

    @Autowired
    private Indexservice indexservice;

    @Autowired
    private Photoservice photoservice;

    @Autowired
    private Pageservice pageservice;

    @RequestMapping("/index")
    public Object index(Model model){
        List<IndexArticle> articles=indexservice.findindex_article();
        List<Article> articleList=new ArrayList<>();
        for(IndexArticle indexArticle:articles){
            for(Article article:indexArticle.getArticles()){
                articleList.add(article);
            }
        }
        model.addAttribute("list",articleList);

        List<String> pathlist=new ArrayList<>();
        for(Article article:articleList){
            int id=(int)article.getId();
            Photo photo=photoservice.findpathbyid(id);
            pathlist.add(photo.getPath());
        }
//        Collections.reverse(pathlist);
        model.addAttribute("pathlist",pathlist);

        List<String> articlepath=new ArrayList<>();
        for(Article article:articleList){
            int id=(int)article.getId();
            Page page=pageservice.findpagepath(id);
            articlepath.add(page.getArticlePath());
        }
        model.addAttribute("articlepath",articlepath);
//        System.out.println(articlepath);
        return "index";
    }
}
